package com.zxl.live.tools.guide;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.ViewParent;
import android.view.WindowManager;

import com.zxl.live.tools.R;
import com.zxl.live.tools.common.ApplicationHelper;

/**
 * Created by Administrator on 2016/8/16.
 */
public class CustomToast implements Runnable {

    private int mShowDuration = 2000;

    private View mShowView;

    private View mBufferView;

    private WindowManager mWindowManager;

    private WindowManager.LayoutParams mLayoutParams;

    private DialogInterface.OnDismissListener mListener;

    public CustomToast() {
        mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mLayoutParams.flags = 136;
        mLayoutParams.format = PixelFormat.TRANSLUCENT;
        mLayoutParams.windowAnimations = R.style.toast_get_score;
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mLayoutParams.setTitle("Toast");
        mWindowManager = (WindowManager) ApplicationHelper.getContext().getSystemService(Context.WINDOW_SERVICE);
    }

    public void setContentView(View mShowView) {
        this.mBufferView = mShowView;
    }

    public void show() {
        if (null != mBufferView) {
            dismiss();
            mShowView = mBufferView;
            ApplicationHelper.postRunInUiThread(this);
            mBufferView = null;
        } else {
            throw new RuntimeException("没有设置显示的控件");
        }
    }

    public void setToastSize(int width, int height) {
        mLayoutParams.width = width;
        mLayoutParams.height = height;
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        mListener = listener;
    }

    public void setWindowAnim(int anim) {
        mLayoutParams.windowAnimations = anim;
    }

    public void setGravity(int gravity) {
        mLayoutParams.gravity = gravity;
    }

    public void setShowToastDuration(int mShowDuration) {
        this.mShowDuration = mShowDuration;
    }

    private boolean removeView() {
        if (mShowView != null) {
            ViewParent parent = mShowView.getParent();
            if (null != parent) {
                try {
                    mWindowManager.removeView(mShowView);
                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    mShowView = null;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public void run() {
        try {
            mWindowManager.addView(mShowView, mLayoutParams);
            if (mShowDuration == -1) {
                return;
            }
            ApplicationHelper.postThreadDelayed(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            }, mShowDuration);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void dismiss() {
        if (removeView()) {
            ApplicationHelper.removeFromUiThread(this);
            if (null != mListener) {
                ApplicationHelper.postRunInUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mListener.onDismiss(null);
                    }
                });
            }
        }
    }
}
